﻿using System;
using UniMana.Data;
using UniMana.Utils.Data;
using System.Data;
using UniMana.Utils.Configs;
using DevExpress.XtraEditors;

namespace UniMana.Win.Training
{
    public partial class FrmMultiRegistAgain : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_data = new DBMain();
        private DataTable m_tblTable;
        private DataTable m_tblSubjectDepartment;
        Training_Student_ClassInfo classinfo;
        Catalogue_Course_SchoolYear_TermInfo schoolterminfo;
        private Training_TrainingProgramInfo programinfo;
        private int m_iFirstRow = -1;

        private DataTable m_tblStudent;
        private DataTable m_tblStudentRegist;

        private int m_iTraining_SubjectOpenKey = 0;
        private int m_iTraining_SubjectKey = 0;
        private int m_iTraining_ProgramKey = 0;

        public DevExpress.Utils.WaitDialogForm dlg; 

        int _flagCheck = 1; // hoc lai = 1, cai thien = 2

        public FrmMultiRegistAgain(int m_iTraining_SubjectKey, int m_iTraining_ProgramKey)
        {
            InitializeComponent();
            this.m_iTraining_SubjectKey = m_iTraining_SubjectKey;
            this.m_iTraining_ProgramKey = m_iTraining_ProgramKey;
            if (m_iTraining_SubjectKey > 0)
                this.m_iTraining_SubjectOpenKey = m_data.m_DCReadOnly.Training_Subject_Opens.GetSubjectOpenKey(m_iTraining_SubjectKey);
        }

        /// <summary>
        /// Form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void FrmMultiRegistAgain_Load(object sender, EventArgs e)
        {
            rluTraining_Class1.GetData(m_data.m_DCReadOnly.Training_Classes, "Training_ClassKey", "Name");
            rluTraining_Class2.GetData(m_data.m_DCReadOnly.Training_Classes, "Training_ClassKey", "Name");

            m_tblTable = new DataTable();
            m_tblTable = m_data.m_DCReadOnly.Training_Subjects.GetSubjectOpenInTrainingProgram(m_data.m_DCReadOnly.Training_TrainingProgram_Details,
                m_data.m_DCReadOnly.Training_Subject_Opens,
                m_data.m_DCReadOnly.Training_TrainingPrograms).ToDataTable();
            if (m_tblTable.Rows.Count != 0)
            {
                m_tblSubjectDepartment = new DataTable();
                m_tblSubjectDepartment.Columns.Add("RowKey");
                m_tblSubjectDepartment.Columns.Add("Training_SubjectKey");
                m_tblSubjectDepartment.Columns.Add("ID");
                m_tblSubjectDepartment.Columns.Add("Name");
                m_tblSubjectDepartment.Columns.Add("TotalCredits");
                m_tblSubjectDepartment.Columns.Add("Training_TrainingProgramKey");
                m_tblSubjectDepartment.Columns.Add("ProgramID");
                m_tblSubjectDepartment.Columns.Add("ProgramName");
                for (int i = 0; i < m_tblTable.Rows.Count; i++)
                {
                    m_tblSubjectDepartment.Rows.Add(i, m_tblTable.Rows[i]["Training_SubjectKey"], m_tblTable.Rows[i]["ID"], m_tblTable.Rows[i]["Name"], m_tblTable.Rows[i]["TotalCredits"], m_tblTable.Rows[i]["Training_TrainingProgramKey"], m_tblTable.Rows[i]["ProgramID"], m_tblTable.Rows[i]["ProgramName"]);
                }
                gluTraining_Subject.GetData(m_tblSubjectDepartment, "RowKey", "Name");
            }

            if ((m_iTraining_SubjectKey > 0) && (m_iTraining_ProgramKey > 0))
            {
                gluTraining_Subject.EditValue = m_iTraining_SubjectKey;
                programinfo = new Training_TrainingProgramInfo(m_iTraining_ProgramKey);
                // Hiển thị tên CTĐT
                if (programinfo.Properties.Training_TrainingProgramKey > 0)
                    txtTraining_TrainingProgram.Text = programinfo.Properties.Name;
                // Hiển thị tên môn học
                for (int i = 0; i < m_tblSubjectDepartment.Rows.Count; i++)
                    if ((m_tblSubjectDepartment.Rows[i]["Training_TrainingProgramKey"].ToString() == m_iTraining_ProgramKey.ToString()) && (m_tblSubjectDepartment.Rows[i]["Training_SubjectKey"].ToString() == m_iTraining_SubjectKey.ToString()))
                        gluTraining_Subject.EditValue = int.Parse(m_tblSubjectDepartment.Rows[i]["RowKey"].ToString());
            }
            LoadStudent();
        }

        /// <summary>
        /// Lấy danh sách sinh viên chưa đăng ký và đăng ký
        /// </summary>
        /// <param name="m_iTraining_SubjectKey"></param>

        private void LoadStudent()
        {
            if ((m_iTraining_SubjectKey > 0) && (m_iTraining_ProgramKey > 0))
            {
                LoadStudentNotRegist();
                LoadStudentRegist();
            }
        }

        /// <summary>
        /// Chọn môn và load lại dữ liệu sinh viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void gluTraining_Subject_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                DataRowView rowview = (DataRowView)gluTraining_Subject.GetSelectedDataRow();
                m_iTraining_ProgramKey = int.Parse(rowview.Row["Training_TrainingProgramKey"].ToString());
                m_iTraining_SubjectKey = int.Parse(rowview.Row["Training_SubjectKey"].ToString());
                m_iTraining_SubjectOpenKey = m_data.m_DCReadOnly.Training_Subject_Opens.GetSubjectOpenKey(m_iTraining_SubjectKey);
                txtTraining_TrainingProgram.Text = rowview.Row["ProgramName"].ToString();
                LoadStudent();
            }
            catch { }
        }

        /// <summary>
        /// Xem danh sách sinh viên đăng ký và chưa đăng ký môn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnView_Click(object sender, EventArgs e)
        {
            if (m_iTraining_SubjectKey > 0)
                LoadStudent();
            else
                XtraMessageBox.Show("Chưa chọn môn");
        }

        /// <summary>
        /// Xem danh sách sinh viên cần đăng ký học lại
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void radReTest_CheckedChanged(object sender, EventArgs e)
        {
            if (radReTest.Checked == true)
            {
                _flagCheck = 1;
                grcStudentList.Text = "Danh sách sinh viên cần đăng ký học lại";
                if ((m_iTraining_SubjectKey > 0) && (m_iTraining_ProgramKey > 0))
                    LoadStudent();
            }
        }

        /// <summary>
        /// Xem danh sách sinh viên cần học cải thiện điểm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void radImproving_CheckedChanged(object sender, EventArgs e)
        {
            if (radImproving.Checked == true)
            {
                _flagCheck = 2;
                grcStudentList.Text = "Danh sách sinh viên cần đăng ký học cải thiện điểm";
                if ((m_iTraining_SubjectKey > 0) && (m_iTraining_ProgramKey > 0))
                    LoadStudent();
            }
        }

        /// <summary>
        /// Danh sách sinh viên chưa đăng ký môn
        /// </summary>

        private void LoadStudentNotRegist()
        {
            m_tblStudent = new DataTable();
            m_tblStudent = m_data.m_DCReadOnly.Training_Students.GetListStudentNotRegistSubjectAgaint(m_data.m_DCReadOnly.Training_Student_Subject_Classes,
                m_data.m_DCReadOnly.Training_Subject_Classes,
                m_data.m_DCReadOnly.Training_Classes,
                m_data.m_DCReadOnly.Training_Student_Classes,
                m_data.m_DCReadOnly.Training_Student_Subjects,
                m_iTraining_SubjectKey,
                m_iTraining_SubjectOpenKey,
                m_iTraining_ProgramKey,
                _flagCheck).ToDataTable();
            if (m_tblStudent.Rows.Count != 0)
            {
                gctStudent.DataSource = m_tblStudent;
                grvStudent.ExpandAllGroups();
            }
            else
                gctStudent.DataSource = null;
        }

        /// <summary>
        /// Danh sách sinh viên đã đăng ký môn
        /// </summary>

        private void LoadStudentRegist()
        {
            m_tblStudentRegist = new DataTable();
            m_tblStudentRegist = m_data.m_DCReadOnly.Training_Students.GetListStudentRegistSubjectAgaint(m_data.m_DCReadOnly.Training_Student_Subjects,
                m_data.m_DCReadOnly.Training_Student_Classes,
                m_iTraining_SubjectKey,
                m_iTraining_SubjectOpenKey,
                m_iTraining_ProgramKey,
                _flagCheck).ToDataTable();
            if (m_tblStudentRegist.Rows.Count != 0)
            {
                gctStudentRegist.DataSource = m_tblStudentRegist;
                grvStudentRegist.ExpandAllGroups();
            }
            else
                gctStudentRegist.DataSource = null;
        }

        /// <summary>
        /// Đăng ký
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (grvStudent.SelectedRowsCount > 0)
            {
                dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
                int[] index = grvStudent.GetSelectedRows();
                for (int i = 0; i < index.Length; i++)
                {
                    if (index[i] >= 0)
                    {
                        classinfo = new Training_Student_ClassInfo(grvStudent.GetRowCellValue(index[i], "Training_StudentKey").ToString(),
                            int.Parse(grvStudent.GetRowCellValue(index[i], "Training_ClassKey").ToString()),
                            "nguyen.tuanan");
                        schoolterminfo = new Catalogue_Course_SchoolYear_TermInfo(int.Parse(classinfo.Properties.Catalogue_CourseKey.ToString()));
                        Training_Student_SubjectInfo sub = new Training_Student_SubjectInfo();
                        sub.Properties.Training_StudentKey = Guid.Parse(grvStudent.GetRowCellValue(index[i], "Training_StudentKey").ToString());
                        sub.Properties.Training_SubjectKey = m_iTraining_SubjectKey;
                        sub.Properties.RegistTime = SysDateTime.Now;
                        sub.Properties.TotalCredits = this.m_data.m_DCReadOnly.Training_Subjects.GetTotalCreditsOfSubject(m_iTraining_SubjectKey);
                        sub.Properties.Training_Student_Subject_Regist_TypeKey = _flagCheck;
                        sub.Properties.Training_SubjectOpenKey = m_data.m_DCReadOnly.Training_Subject_Opens.GetSubjectOpenKey(m_iTraining_SubjectKey);
                        sub.Properties.Training_TrainingProgramKey = m_iTraining_ProgramKey;
                        sub.Save();                        
                    }
                }
                LoadStudent();
                dlg.Close();
            }
            else
                XtraMessageBox.Show("Chưa chọn sinh viên", "Thông báo");
        }

        /// <summary>
        /// Hủy đăng ký
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (grvStudentRegist.SelectedRowsCount > 0)
            {
                dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
                int[] index = grvStudentRegist.GetSelectedRows();
                //int _d = 0;
                for (int i = 0; i < index.Length; i++)
                {
                    if (index[i] >= 0)
                    {
                        Training_Student_SubjectInfo studentsubject = new Training_Student_SubjectInfo(grvStudentRegist.GetRowCellValue(index[i], "Training_StudentKey").ToString(), m_iTraining_SubjectKey, _flagCheck, m_iTraining_ProgramKey, m_iTraining_SubjectOpenKey);
                        if (studentsubject.Properties.Training_Student_SubjectKey > 0)
                            studentsubject.Delete();
                    }
                }
                LoadStudent();
                dlg.Close();
            }
            else
                XtraMessageBox.Show("Chưa chọn sinh viên", "Thông báo");
        }

        //int groupKeyFirst = 0;
        //private void grvStudent_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        //{
            //if (grvStudent.FocusedRowHandle >= 0)
            //{
            //    try
            //    {
            //        int[] row = grvStudent.GetSelectedRows();
            //        if (row.Length > 1)
            //        {
            //            int groupKey = Convert.ToInt16(this.grvStudent.GetRowCellValue(this.m_iFirstRow, gclClass).ToString());
            //            bool flag = false;
            //            for (int i = 0; i < row.Length; i++)
            //            {
            //                if (row[i] != this.m_iFirstRow)
            //                {
            //                    if (row[i] <= 0)
            //                    {
            //                        this.grvStudent.UnselectRow(row[i]);
            //                    }
            //                    else
            //                    {
            //                        int groupKeyCheck = Convert.ToInt16(this.grvStudent.GetRowCellValue(row[i], gclClass).ToString());
            //                        if (groupKey != groupKeyCheck)
            //                        {
            //                            flag = true;
            //                            this.grvStudent.UnselectRow(row[i]);
            //                            this.grvStudent.SelectRow(m_iFirstRow);
            //                        }
            //                    }
            //                }
            //            }
            //            if (groupKey != groupKeyFirst)
            //            {
            //                if (flag)
            //                {
            //                    XtraMessageBox.Show("Không thể chọn cùng lúc nhiều sinh viên khác lớp");
            //                    groupKeyFirst = groupKey;
            //                }
            //            }
            //        }
            //        else
            //        {
            //            this.m_iFirstRow = this.grvStudent.FocusedRowHandle;
            //        }
            //    }
            //    catch { }
            //}
        //}
    }
}
